package stardust.product.platform.webc.log;

import stardust.product.platform.webc.WebcRequestHandler;
import stardust.product.platform.webc.spi.servlet.ServletWebcRequestHandler;


public class RequestSnapshot
{
  
  private long accessTime;

  private String clientAddress;
  
  private String sourceRequest;
  
  private long consumptionTime;
  
  public RequestSnapshot()
  {
    ;
  }
  
  public RequestSnapshot(String clientAddress, String sourceRequest)
  {
    this.clientAddress = clientAddress;
    this.sourceRequest = sourceRequest;
    
    this.accessTime = System.currentTimeMillis();
  }

  public long getAccessTime()
  {
    return accessTime;
  }

  public void setAccessTime(long accessTime)
  {
    this.accessTime = accessTime;
  }

  public String getClientAddress()
  {
    return clientAddress;
  }

  public void setClientAddress(String clientAddress)
  {
    this.clientAddress = clientAddress;
  }

  public String getSourceRequest()
  {
    return sourceRequest;
  }

  public void setSourceRequest(String sourceRequest)
  {
    this.sourceRequest = sourceRequest;
  }
  
  public long getConsumptionTime()
  {
    return consumptionTime;
  }

  public void setConsumptionTime(long consumptionTime)
  {
    this.consumptionTime = consumptionTime;
  }

  public static RequestSnapshot createRequestSnapshot(WebcRequestHandler request)
  {
    RequestSnapshot snapshot = new RequestSnapshot("UNKNOW", "UNKNOW");
    
    if (request instanceof ServletWebcRequestHandler)
    {
      ServletWebcRequestHandler req = (ServletWebcRequestHandler) request;
      
      snapshot.setAccessTime(System.currentTimeMillis());
      snapshot.setClientAddress(req.getRequest().getRemoteHost() + ":" + req.getRequest().getRemotePort());
      snapshot.setSourceRequest(req.getMultipartContentRequestSupport().toString());
    }
    
    return snapshot;
  }
  
}
